package fpTree;

import java.util.HashMap;
import java.util.Map;

public class Node implements Comparable<Node>
{
	public int itemName = -1;
	private int count = -9999999;
	private Node nodeLink;
	public Node parent;
	private Map<Integer, Node> children;
	
	public Map<Integer, Node> getChildren()
	{
		if (this.children == null)
		{
			this.children = new HashMap<Integer, Node>();
		}
		return this.children;
	}
	
	public void setChildren(Map<Integer, Node> children)
	{
		this.children = children;
	}
	
	@Override
	public String toString()
	{
		return new StringBuilder().append(this.itemName).append("-").append(this.getCount()).toString();
	}
	
	@Override
	public int compareTo(Node o)
	{
		if (this.getCount() != o.getCount())
		{
			return o.getCount() - this.getCount();
		} else
		{
			return this.itemName - o.itemName;
		}
	}
	
	public void setNodeLink(Node nodeLink)
	{
		this.nodeLink = nodeLink;
	}
	
	public Node getNodeLink()
	{
		return this.nodeLink;
	}

	public void setCount(int count)
	{
		this.count = count;
	}

	public int getCount()
	{
		return count;
	}

	public String printTree()
	{
		StringBuffer sb = new StringBuffer();
		printTree(">> ", sb);
		return sb.toString();
	}
	
	public String printTree(String spacing)
	{
		StringBuffer sb = new StringBuffer();
		printTree(spacing + ">> ", sb);
		return sb.toString();
	}
	
	public void printTree(String spacing, StringBuffer sb)
	{
		sb.append(spacing).append(this).append("\n");
		for (Node child : this.getChildren().values())
		{
			child.printTree(spacing + "    ", sb);
		}
	}
	
	public Node shallowCopy()
	{
		Node newNode = new Node();
		newNode.itemName = this.itemName;
		newNode.count = this.count;
		return newNode;
	}

}
